package com.hwtx.form.domain.handle;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.hwtx.form.annotation.Action;
import com.hwtx.form.annotation.FormType;
import com.hwtx.form.annotation.Log;
import com.hwtx.form.annotation.OperationType;
import com.hwtx.form.domain.def.FormDef;
import com.hwtx.form.domain.repo.SysUserRepo;
import com.hwtx.form.domain.service.RacService;
import com.hwtx.form.domain.vo.FormData;
import com.hwtx.form.domain.vo.FormRepData;
import com.hwtx.form.query.FormValueQuery;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.system.dto.UserDto;
import io.geekidea.boot.system.query.SysUserQuery;
import io.geekidea.boot.system.vo.SysUserVo;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.beans.BeanMap;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.hwtx.form.domain.FormConstants.USER_FORM_HANDLE_NAME;
import static com.hwtx.form.util.JacksonUtil.map2Oject;

@FormType(USER_FORM_HANDLE_NAME)
public class UserFormDataHandle implements FormDataHandle {

    @Resource
    private SysUserRepo sysUserRepo;
    @Resource
    private RacService racService;

    @Override
    public FormRepData getFormData(FormDef formDef, FormValueQuery formValueQuery) {
        Long userId = formValueQuery.getValueIds().getFirst();
        SysUserVo sysUserVo = sysUserRepo.getSysUserById(userId);
        sysUserVo.setRoleIds(racService.getRolesByUserId(userId));
        sysUserVo.setOrgIds(Joiner.on(",").join(racService.getOrgIdsByUserId(userId)));
        return FormRepData.builder().data(BeanMap.create(sysUserVo)).build();
    }

    @Action
    @Log(type = OperationType.GET, value = "查询机构已分配的用户")
    public Paging<SysUserVo> orgUserAssigned(Map<String, Object> formData) {
        SysUserQuery query = map2Oject(formData, SysUserQuery.class);
        if (Objects.equals(query.getOrgId(), -1L)) {
            query.setOrgId(null);
        }
        return racService.getAssignedOrgUsersPage(query);
    }

    @Override
    public Object saveFormData(FormDef formDef, FormData formData) {
        Object roleIds = formData.getData().get("roleIds");
        Object orgIds = formData.getData().get("orgIds");
        if (roleIds instanceof String) {
            formData.getData().put("roleIds", getLongList((String) roleIds));
        }
        if (orgIds instanceof String) {
            formData.getData().put("orgIds", getLongList((String) orgIds));
        }
        UserDto userDto = map2Oject(formData.getData(), UserDto.class);
        assert userDto != null;
        return racService.saveUserInfo(userDto);
    }

    private List<Long> getLongList(String item) {

        if (StringUtils.isNotEmpty(item)) {
            return Stream.of(item.split(",")).map(Long::parseLong).collect(Collectors.toList());
        } else {
            return Lists.newArrayList();
        }
    }

    @Override
    public Object removeFormData(FormDef formDef, FormValueQuery formValueQuery) {
        if (CollectionUtils.isNotEmpty(formValueQuery.getValueIds())) {
            return racService.deleteUser(formValueQuery.getValueIds().getFirst());
        }
        return false;
    }
}